package com.fr.cell;

import com.fr.cell.core.GridUtils;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.KeyStroke;

public class GridKeyAction extends AbstractAction
{
  private Grid grid;
  private String actionKey;
  private boolean isShift;

  public GridKeyAction(Grid paramGrid, String paramString, boolean paramBoolean)
  {
    this.grid = paramGrid;
    this.actionKey = paramString;
    this.isShift = paramBoolean;
  }

  protected static void initGridInputActionMap(Grid paramGrid)
  {
    InputMap localInputMap = paramGrid.getInputMap();
    ActionMap localActionMap = paramGrid.getActionMap();
    localInputMap.put(KeyStroke.getKeyStroke(37, 0), "left");
    localActionMap.put("left", new GridKeyAction(paramGrid, "left", false));
    localInputMap.put(KeyStroke.getKeyStroke(37, 1), "left_shift");
    localActionMap.put("left_shift", new GridKeyAction(paramGrid, "left", true));
    localInputMap.put(KeyStroke.getKeyStroke(39, 0), "right");
    localActionMap.put("right", new GridKeyAction(paramGrid, "right", false));
    localInputMap.put(KeyStroke.getKeyStroke(39, 1), "right_shift");
    localActionMap.put("right_shift", new GridKeyAction(paramGrid, "right", true));
    localInputMap.put(KeyStroke.getKeyStroke(38, 0), "up");
    localActionMap.put("up", new GridKeyAction(paramGrid, "up", false));
    localInputMap.put(KeyStroke.getKeyStroke(38, 1), "up_shift");
    localActionMap.put("up_shift", new GridKeyAction(paramGrid, "up", true));
    localInputMap.put(KeyStroke.getKeyStroke(40, 0), "down");
    localActionMap.put("down", new GridKeyAction(paramGrid, "down", false));
    localInputMap.put(KeyStroke.getKeyStroke(40, 1), "down_shift");
    localActionMap.put("down_shift", new GridKeyAction(paramGrid, "down", true));
    localInputMap.put(KeyStroke.getKeyStroke(10, 0), "enter");
    localActionMap.put("enter", new GridKeyAction(paramGrid, "enter", false));
    localInputMap.put(KeyStroke.getKeyStroke(10, 1), "enter_shift");
    localActionMap.put("enter_shift", new GridKeyAction(paramGrid, "enter", true));
    localInputMap.put(KeyStroke.getKeyStroke(9, 0), "tab");
    localActionMap.put("tab", new GridKeyAction(paramGrid, "tab", false));
    localInputMap.put(KeyStroke.getKeyStroke(9, 1), "tab_shift");
    localActionMap.put("tab_shift", new GridKeyAction(paramGrid, "tab", true));
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    if ("left".equals(this.actionKey))
      selectionMove(2, true);
    else if ("right".equals(this.actionKey))
      selectionMove(4, true);
    else if ("up".equals(this.actionKey))
      selectionMove(1, true);
    else if ("down".equals(this.actionKey))
      selectionMove(3, true);
    else if ("enter".equals(this.actionKey))
      enterKeyPressed();
    else if ("tab".equals(this.actionKey))
      tabKeyPressed();
  }

  public void enterKeyPressed()
  {
    if (this.grid.isCellEditing())
      this.grid.stopEditing();
    int i = this.grid.getEnterMoveDirection();
    if (i == 0)
      i = 3;
    selectionMove(i, false);
  }

  public void tabKeyPressed()
  {
    if (this.grid.isCellEditing())
      this.grid.stopEditing();
    int i = this.grid.getTabMoveDirection();
    if (i == 0)
      i = 4;
    selectionMove(i, false);
  }

  private void selectionMove(int paramInt, boolean paramBoolean)
  {
    ReportPane localReportPane = this.grid.getReportPane();
    GridSelection localGridSelection = localReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
      return;
    if (paramInt == 3)
      if (this.isShift)
        if (paramBoolean)
          unionDown(localReportPane, localGridSelection);
        else
          up(localReportPane, localGridSelection);
      else
        down(localReportPane, localGridSelection);
    else if (paramInt == 4)
      if (this.isShift)
        if (paramBoolean)
          unionRight(localReportPane, localGridSelection);
        else
          left(localReportPane, localGridSelection);
      else
        right(localReportPane, localGridSelection);
    else if (paramInt == 1)
      if (this.isShift)
        if (paramBoolean)
          unionUp(localReportPane, localGridSelection);
        else
          down(localReportPane, localGridSelection);
      else
        up(localReportPane, localGridSelection);
    else if (paramInt == 2)
      if (this.isShift)
        if (paramBoolean)
          unionLeft(localReportPane, localGridSelection);
        else
          right(localReportPane, localGridSelection);
      else
        left(localReportPane, localGridSelection);
    localReportPane.repaint();
    localReportPane.fireGridSelectionChanged();
  }

  private void left(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle = paramGridSelection.getEditRectangle();
    if (localRectangle.x > 0)
    {
      int i = localRectangle.x - 1;
      GridUtils.doSelectCell(paramReportPane, i, localRectangle.y);
      paramReportPane.ensureColumnRowVisible(i, localRectangle.y);
    }
  }

  private void right(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle = paramGridSelection.getEditRectangle();
    int i = localRectangle.x + localRectangle.width;
    GridUtils.doSelectCell(paramReportPane, i, localRectangle.y);
    paramReportPane.ensureColumnRowVisible(i, localRectangle.y);
  }

  private void up(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle = paramGridSelection.getEditRectangle();
    if (localRectangle.y > 0)
    {
      int i = localRectangle.y - 1;
      GridUtils.doSelectCell(paramReportPane, localRectangle.x, i);
      paramReportPane.ensureColumnRowVisible(localRectangle.x, i);
    }
  }

  private void down(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle = paramGridSelection.getEditRectangle();
    int i = localRectangle.y + localRectangle.height;
    GridUtils.doSelectCell(paramReportPane, localRectangle.x, i);
    paramReportPane.ensureColumnRowVisible(localRectangle.x, i);
  }

  protected void unionLeft(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle1 = paramGridSelection.getEditRectangle();
    Rectangle localRectangle2 = paramGridSelection.getLastCellRectangle();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    int i = localRectangle1.x + localRectangle1.width - 1;
    int j = localRectangle2.x + localRectangle2.width - 1;
    Rectangle localRectangle3 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(j, localRectangle2.y, 1, localRectangle2.height));
    if ((j <= i) || (localRectangle3.contains(localRectangle1)))
    {
      if (localRectangle1.x <= 0)
        return;
      Rectangle localRectangle4 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x - 1, localRectangle2.y, localRectangle2.width + 1, localRectangle2.height));
      localRectangle2.setBounds(localRectangle4.union(localRectangle2));
      paramReportPane.ensureColumnRowVisible(localRectangle2.x, localRectangle2.y);
    }
    else
    {
      localRectangle2.width = (localRectangle3.x - localRectangle2.x);
      paramReportPane.ensureColumnRowVisible(localRectangle2.x + localRectangle2.width - 1, localRectangle2.y);
    }
  }

  protected void unionRight(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle1 = paramGridSelection.getEditRectangle();
    Rectangle localRectangle2 = paramGridSelection.getLastCellRectangle();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    Rectangle localRectangle3 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x, localRectangle2.y, 1, localRectangle2.height));
    if ((localRectangle2.x >= localRectangle1.x) || (localRectangle3.contains(localRectangle1)))
    {
      if ((!(paramReportPane.isColumnEndless())) && (localRectangle2.x + localRectangle2.width > localTemplateReport.getColumnCount() - 1))
        return;
      Rectangle localRectangle4 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x, localRectangle2.y, localRectangle2.width + 1, localRectangle2.height));
      localRectangle2.setBounds(localRectangle4.union(localRectangle2));
      paramReportPane.ensureColumnRowVisible(localRectangle2.x + localRectangle2.width - 1, localRectangle2.y);
    }
    else
    {
      localRectangle2.width = (localRectangle2.x + localRectangle2.width - localRectangle3.x + localRectangle3.width);
      localRectangle2.x = (localRectangle3.x + localRectangle3.width);
      paramReportPane.ensureColumnRowVisible(localRectangle2.x + 1, localRectangle2.y);
    }
  }

  protected void unionUp(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle1 = paramGridSelection.getEditRectangle();
    Rectangle localRectangle2 = paramGridSelection.getLastCellRectangle();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    int i = localRectangle1.y + localRectangle1.height - 1;
    int j = localRectangle2.y + localRectangle2.height - 1;
    Rectangle localRectangle3 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x, j, localRectangle2.width, 1));
    if ((j <= i) || (localRectangle3.contains(localRectangle1)))
    {
      if (localRectangle2.y <= 0)
        return;
      Rectangle localRectangle4 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x, localRectangle2.y - 1, localRectangle2.width, localRectangle2.height + 1));
      localRectangle2.setBounds(localRectangle4.union(localRectangle2));
      paramReportPane.ensureColumnRowVisible(localRectangle2.x, localRectangle2.y);
    }
    else
    {
      localRectangle2.height = (localRectangle3.y - localRectangle2.y);
      paramReportPane.ensureColumnRowVisible(localRectangle2.x, localRectangle2.y + localRectangle2.height - 1);
    }
  }

  protected void unionDown(ReportPane paramReportPane, GridSelection paramGridSelection)
  {
    Rectangle localRectangle1 = paramGridSelection.getEditRectangle();
    Rectangle localRectangle2 = paramGridSelection.getLastCellRectangle();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    Rectangle localRectangle3 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x, localRectangle2.y, localRectangle2.width, 1));
    if ((localRectangle2.y >= localRectangle1.y) || (localRectangle3.contains(localRectangle1)))
    {
      if ((!(paramReportPane.isRowEndless())) && (localRectangle2.y + localRectangle2.height > localTemplateReport.getRowCount() - 1))
        return;
      Rectangle localRectangle4 = this.grid.caculateIntersectsUnion(localTemplateReport, new Rectangle(localRectangle2.x, localRectangle2.y, localRectangle2.width, localRectangle2.height + 1));
      localRectangle2.setBounds(localRectangle4.union(localRectangle2));
      paramReportPane.ensureColumnRowVisible(localRectangle2.x, localRectangle2.y + localRectangle2.height - 1);
    }
    else
    {
      localRectangle2.height = (localRectangle2.y + localRectangle2.height - localRectangle3.y + localRectangle3.height);
      localRectangle2.y = (localRectangle3.y + localRectangle3.height);
      paramReportPane.ensureColumnRowVisible(localRectangle2.x, localRectangle2.y + 1);
    }
  }
}